var mithrilui_regex={
    "fno":{
        "desc":"集团工号",
        "regex":/^([A-Z]|[a-z]|\d){1,}(\d{3,17}|(\d{4,17}[A-Z]\d{0,4}))|(^\d{18}$)|(^\d{17}(\d|X|x)$)$/
    },
    "ftel":{
        "desc":"集团分机",
        "regex":/(^\d{5,6}$)|(^\d{3}-\d{5,6}$)|(^\d{8,9}$)/
    },
    "fmail":{
        "desc":"集团邮箱",
        "regex":/^([a-zA-Z]|[0-9])(\w|\-)+@(mail.foxconn.com)|(foxconn.com)$/
    },
    "sid":{
        "desc":"身份证号码",
        "regex":/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    },
    "mail":{
        "desc":"个人邮箱",
        "regex":/^([a-zA-Z0-9]+[_|_|\-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/
    },
    "mobile":{
        "desc":"手机号码",
        "regex":/(^\d{11}$)|(^([6|9])\d{7}$)|(^[6]([8|6])\d{5})/
    }
}

var mithrilui={
    validate:function(elem){
        //系统默认正则
        if(mithrilui_regex[elem.getAttribute("regex")]){
            var patt=new RegExp(mithrilui_regex[elem.getAttribute("regex")].regex);
            if(!patt.test(elem.value)){
                mithrilui.ShowErr(elem,mithrilui_regex[elem.getAttribute("regex")].desc+"的格式错误");
            }else{
                mithrilui.HideErr(elem);
            }
        }
        //字数匹配
        if(elem.getAttribute("regex").indexOf("len")==0){
            var sub=elem.getAttribute("regex").split(":");
            var min=sub[1];
            var max=sub[2];
            var count=mithrilui.CountChar(elem.value);
            // console.log(count);
            if(count<min || count>max){
                mithrilui.ShowErr(elem,"当前字数"+count+"个(一个汉字占3个字),不在"+min+"~"+max+"之间！");
            }else{
                mithrilui.HideErr(elem);
            }
        }
        //自定义正则
        if(elem.getAttribute("regex").indexOf("^")==0){
            // console.log("test go /");
            // console.log(elem);
            var patt=new RegExp(elem.getAttribute("regex"));
            if(!patt.test(elem.value)){
                mithrilui.ShowErr(elem,"格式错误");
            }else{
                mithrilui.HideErr(elem);
            }
        }
    },
    //显示控件错误
    ShowErr:function(elem,msg){
        elem.parentNode.querySelector("p").innerText=msg;
    },
    //不显示控件错误
    HideErr:function(elem){
        elem.parentNode.querySelector("p").innerText="";
    },
    //字数统计
    CountChar:function(words){
        var num=0;
        for (i = 0; i < words.length; i++) {
            var _c=words.charAt(i);
            if(_c.match(/^[\u0000-\u00ff]$/)){
                //英文数字及符号
                num++;
            }else{
                //中文
                num+=3;
            }
        }
        return num;
    },
    //提交验证
    SubmitCheck:function(){
        var inputs=document.querySelectorAll("input,textarea");
        for(var i=0;i<inputs.length;i++){
            if(inputs[i].getAttribute("regex")){
                inputs[i].focus();
                inputs[i].blur();
            }
        }
        for(var i=0;i<inputs.length;i++){
            if(inputs[i].getAttribute("regex")){
                if(inputs[i].parentNode.querySelector("p").innerText!=""){
                    return false;
                }
            }
        }
        return true;
    }
}

var minput={
    attrs:{},
    oninit:function(vnode){
        minput.attrs=vnode.attrs;
    },
    oncreate:function(vnode){
        // console.log(vnode.attrs);
        vnode.dom.setAttribute("class",vnode.attrs.className);
        vnode.dom.querySelector("input").removeAttribute("class");
        if(vnode.attrs.regex){
            vnode.dom.querySelector("input").addEventListener("blur",minput.Blur);
        }
    },
    view:function(){
        return m("div",[
            m("input",minput.attrs),
            m("p")
        ])
    },
    Blur:function(){
        // console.log(this.getAttribute("regex"));
        mithrilui.validate(this);
    }
}

var mtextarea={
    attrs:{},
    oninit:function(vnode){
        mtextarea.attrs=vnode.attrs;
    },
    oncreate:function(vnode){
        // console.log(vnode.attrs);
        vnode.dom.setAttribute("class",vnode.attrs.className);
        vnode.dom.querySelector("textarea").removeAttribute("class");
        if(vnode.attrs.regex){
            vnode.dom.querySelector("textarea").addEventListener("blur",mtextarea.Blur);
        }
    },
    view:function(){
        return m("div",[
            m("textarea",mtextarea.attrs),
            m("p")
        ])
    },
    Blur:function(){
        // console.log(this.getAttribute("regex"));
        mithrilui.validate(this);
    }
}

var mradio={
    attrs:{},
    oninit:function(vnode){
        mradio.attrs=vnode.attrs;
        //必须属性
        if(typeof vnode.attrs.noactive==="undefined"){
            alert("mradio没有定义noactive");
        }
        if(typeof vnode.attrs.active==="undefined"){
            alert("mradio没有定义active");
        }
        if(typeof vnode.attrs.data==="undefined"){
            alert("mradio没有定义data");
        }
        if(vnode.attrs.data.length>0){
            if(!vnode.attrs.data[0].value || !vnode.attrs.data[0].key){
                alert("mradio没有定义data.key和data.value");
            }
        }
        
    },
    oncreate:function(vnode){
        // console.log(vnode.attrs);
        vnode.dom.setAttribute("class",vnode.attrs.class);
        //带值初始化
        if(vnode.attrs.value){
            vnode.dom.querySelector("input").value=vnode.attrs.value;
        }
    },
    view:function(){
        return m("div",[
            m("div",{
                style:"display:flex; flex-wrap: wrap;",
                noactive:mradio.attrs.noactive,
                active:mradio.attrs.active
            },mradio.attrs.data.map(function(item,index){
                return m("div",{
                    class:mradio.GetActive(item,index),
                    onclick:mradio.Select,
                    value:item.value
                },item.key)
            })),
            m("input[type=hidden]",{id:mradio.attrs.id,value:mradio.attrs.data[0].value}),
            m("p")
        ])
    },
    //选择处理
    Select:function(){
        var divs=this.parentNode.querySelectorAll("div");
        for(let i=0;i<divs.length;i++){
            divs[i].setAttribute("class",this.parentNode.getAttribute("noactive"));
        }
        this.setAttribute("class",this.parentNode.getAttribute("active"))
        this.parentNode.parentNode.querySelector("input").value=this.getAttribute("value");
    },
    //选中初始化
    GetActive:function(item,index){
        if(mradio.attrs.value){
            if(item.value==mradio.attrs.value){
                return mradio.attrs.active;
            }else{
                return mradio.attrs.noactive;
            }
        }else{
            return index==0?mradio.attrs.active:mradio.attrs.noactive
        }
    }
}

var mcheckbox={
    oninit:function(vnode){
        mcheckbox.attrs=vnode.attrs;
         //必须属性
        if(typeof vnode.attrs.noactive==="undefined"){
            alert("mradio没有定义noactive");
        }
        if(typeof vnode.attrs.active==="undefined"){
            alert("mradio没有定义active");
        }
        if(typeof vnode.attrs.data==="undefined"){
            alert("mradio没有定义data");
        }
        if(typeof vnode.attrs.min==="undefined"){
            alert("mcheckbox没有定义min");
        }
        if(typeof vnode.attrs.max==="undefined"){
            alert("mcheckbox没有定义max");
        }
        if(vnode.attrs.data.length>0){
            if(!vnode.attrs.data[0].value || !vnode.attrs.data[0].key){
                alert("mcheckbox没有定义data.key和data.value");
            }
        }
    },
    oncreate:function(vnode){
        // console.log(vnode.attrs);
        vnode.dom.setAttribute("class",vnode.attrs.class);
        //带值初始化
        if(vnode.attrs.value){
            vnode.dom.querySelector("input").value=vnode.attrs.value;
        }
    },
    view:function(){
        return m("div",[
            m("div",{
                style:"display:flex; flex-wrap: wrap;",
                noactive:mcheckbox.attrs.noactive,
                active:mcheckbox.attrs.active,
                max:mcheckbox.attrs.max,
                min:mcheckbox.attrs.min
            },mcheckbox.attrs.data.map(function(item){
                return m("div",{style:"margin-left:1px;",class:mcheckbox.GetActive(item),onclick:mcheckbox.Select,value:item.value},item.key)
            })),
            m("input[type=hidden]",{id:mcheckbox.attrs.id,regex:"area",blur:mcheckbox.Blur}),
            m("p")
        ])
    },
    //blur处理
    Blur:function(){
        var div=this.parentNode.querySelector("div");
        var min=div.getAttribute("min");
        var max=div.getAttribute("max");
        var count=this.value.split(",").length;
        //没有值赋值0
        if(this.value==""){
            count=0;
        }
        console.log(count);
        if(count<min || count>max){
            this.parentNode.querySelector("p").innerText="必须选择"+min+"~"+max+"个";
        }
    },
    //选中处理
    Select:function(){
        //效果更新
        this.setAttribute("class",this.getAttribute("class")==this.parentNode.getAttribute("active")?this.parentNode.getAttribute("noactive"):this.parentNode.getAttribute("active"));

        var val='';
        var count=0;
        var divs=this.parentNode.querySelectorAll("div");
        for(let i=0;i<divs.length;i++){
            if(divs[i].getAttribute("class")==divs[i].parentNode.getAttribute("active")){
                count++;
                if(count>this.parentNode.getAttribute("max")){
                    this.setAttribute("class",this.parentNode.getAttribute("noactive"));
                    mithrilui.ShowErr(this.parentNode,"最多选择"+this.parentNode.getAttribute("max")+"个");
                }else{
                    val=val+(val==""?"":",")+divs[i].getAttribute("value");
                    mithrilui.HideErr(this.parentNode);
                }
            }
        }
        this.parentNode.parentNode.querySelector("input").value=val;
    },
    //选中初始化
    GetActive:function(item){
        if(mcheckbox.attrs.value){
            //拆分val
            var vals=mcheckbox.attrs.value.split(",");
            for(var i=0;i<vals.length;i++){
                if(item.value==vals[i]){
                    return mcheckbox.attrs.active;
                }
            }
            return mcheckbox.attrs.noactive;
        }else{
            return mcheckbox.attrs.noactive;
        }
    }
}

// var mselect={
//     attrs:{},
//     oninit:function(vnode){
//         mselect.attrs=vnode.attrs;
//     },
//     oncreate:function(vnode){
//         console.log(vnode.attrs);
//         vnode.dom.setAttribute("class",vnode.attrs.class);
//     },
//     view:function(){
//         return m("div",[
//             m("input[type=text]",{id:mselect.attrs.id,list:mselect.attrs.id+"_dl",onblur:mselect.Validate,placeholder:"选择或输入关键字"}),
//             m("datalist",{id:mselect.attrs.id+"_dl"},mselect.attrs.data.map(function(item){
//                 return m("option",{value:item.value},item.key)
//             })),
//             m("p")
//         ])
//     },
//     Validate:function() {
//         var data=this.parentNode.querySelector("datalist").querySelectorAll("option");
//         console.log(data);
//         var check=false;
//         for(var i=0;i<data.length;i++){
//             console.log(data[i].value);
//             if(data[i].value==this.value){
//                 check=true;
//             }
//         }
//         if(check){
//             mithrilui.HideErr(this);
//         }else{
//             mithrilui.ShowErr(this,"请选择指定范围数据");
//         }
//     }
// }

//兼容iphone
var mselect={
    attrs:{},
    oninit:function(vnode){
        mselect.attrs=vnode.attrs;
        //必须属性
        if(!vnode.attrs.data){
            alert("mradio没有定义data");
        }
        if(vnode.attrs.data.length>0){
            if(!vnode.attrs.data[0].value || !vnode.attrs.data[0].key){
                alert("mradio没有定义data.key和data.value");
            }
        }
    },
    oncreate:function(vnode){
        console.log(vnode.attrs);
        vnode.dom.setAttribute("class",vnode.attrs.class);
    },
    view:function(){
        return m("div",[
            m("input[type=text]",{
                class:mselect.attrs.class,
                style:"width:100%;",
                regex:"select",
                onblur:mselect.Blur,
                onfocus:mselect.ShowDatalist,
                oninput:mselect.FilterData,
                onclick:mselect.SelectValue
            }),
            m("div",{
                style:"border:solid 1px silver; display:none; height:6rem; overflow:auto;"
            },mselect.attrs.data.map(function(item){
                return m("div",{style:"line-height:2rem;",data:item.value,onclick:mselect.SelectData},item.key)
            })),
            m("input[type=hidden]",{id:mselect.attrs.id}),
            m("p")
        ])
    },
    ShowDatalist:function(){
        this.parentNode.querySelector("div").style.display="block";
    },
    SelectValue:function(){
        this.select();
    },
    FilterData:function(){
        var selects=this.parentNode.querySelector("div").querySelectorAll("div");
        for(var i=0;i<selects.length;i++){
            if(selects[i].innerText.toUpperCase().indexOf(this.value.toUpperCase())==-1){
                selects[i].style.display="none";
            }else{
                selects[i].style.display="block";
            }
        }
    },
    SelectData:function(){
        var inputText=this.parentNode.parentNode.querySelector("input");
        var inputValue=this.parentNode.parentNode.querySelectorAll("input")[1];
        
        inputText.value=this.innerText;
        inputValue.value=this.getAttribute("data");
        //触发inputText事件
        inputText.focus();
        inputText.blur();
        //触发inputValue事件
        inputValue.click();
        //隐藏datalist
        this.parentNode.parentNode.querySelector("div").style.display="none";
    },
    Blur:function(){
        var selects=this.parentNode.querySelector("div").querySelectorAll("div");
        for(var i=0;i<selects.length;i++){
            if(selects[i].innerText.toUpperCase()==this.value.toUpperCase()){
                this.parentNode.querySelector("p").innerText="";
                this.parentNode.querySelector("div").style.display="none";
                return true;
            }
        }
        this.parentNode.querySelector("p").innerText="请选择列表中的数据";
    }
}